﻿<cfsilent>
	<cfscript>

		temFile = GetTempDirectory() & createUUID() & ".xls";
		
		pubId = event.getArg("PubID");
		
		/* 读取出版社目录信息 */
		sql = "	SELECT
					p.publisher
				FROM
					t_publisher p
				WHERE
					p.pid = :pubId ";
		
		queryObj = new Query(datasource = application.dnsSlave);
		queryObj.addParam(name = "pubId", value = pubId, cfsqltype = "cf_sql_char");
		rs_publisherEntity = queryObj.execute(sql=sql).getResult();
		
		downFile = URLEncodedFormat( "教材目录-" & rs_publisherEntity.publisher, "utf-8");

		/* 生成电子表格数据 */		
		sql = "	SELECT
					b.pyid,b.book_name,b.book_edition,b.book_authors,b.book_prise,b.book_isbn
				FROM
					t_book b INNER JOIN t_publisher p ON b.pid = p.pid
				WHERE
					b.pid = :pubId 
				ORDER BY
					p.publisher,b.book_name";
		
		queryObj = new Query(datasource = application.dnsSlave);
		queryObj.addParam(name = "pubId", value = pubId, cfsqltype = "cf_sql_char");
		rs_book = queryObj.execute(sql=sql).getResult();
		
		/* 载入相关操作组件 */
		excel = getProperty("serviceFactory").getBean("spreadSheetObject");
		mathAdvice = getProperty("serviceFactory").getBean("senateMathAdvice");
		dictAdvice = getProperty("serviceFactory").getBean("senateDictionaryAdvice");
	
		/* 创建工作簿 */
		spreadsheetObj = excel.SpreadSheetNew( rs_publisherEntity.publisher, false);
		
		/* 添加表头 */
		excel.SpreadsheetAddrow(spreadsheetObj, "首字母,书名,版次,作者,定价,ISBN,ISBN10");
		
		row = 1;
		
		/* 写入计划任务 */
		for( a = 1; a le rs_book.recordCount; a++ ){
			
			row++;
			
			rowData = rs_book['pyid'][a] 
						& "," & replace( rs_book['book_name'][a], ",", "", "ALL")
						& "," & dictAdvice.getBookEdition( rs_book['book_edition'][a], "")
						& "," & replace( rs_book['book_authors'][a], "/", " | ", "ALL")
						& "," & rs_book['book_prise'][a];
		
			if ( len(rs_book['book_isbn'][a]) eq 10 ) {
				
				rowData = rowData 
							& "," & replace( dictAdvice.getISBN( mathAdvice.upgradeISBN( rs_book['book_isbn'][a] ) ), "ISBN ", "")
							& "," & replace( dictAdvice.getISBN( rs_book['book_isbn'][a] ), "ISBN ", "");
			
			}
			else {
				
				rowData = rowData & "," & replace( dictAdvice.getISBN( rs_book['book_isbn'][a] ), "ISBN ", "");
			}
			
			excel.SpreadsheetAddrow(spreadsheetObj, rowData);
			
		}
		
		excel.SpreadSheetWrite(spreadsheetObj, temFile, true);
	
	</cfscript>
	<cfheader name="Content-Disposition" value="attachment; filename=#downFile#.xls" />
	<cfcontent file="#temFile#" reset="yes" type="application/msexcel" deletefile="yes" />
</cfsilent>